package com.heima.admin.controller.v1;

import com.heima.admin.service.ChannelService;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 控制层
 */
@Api(value = "频道管理API接口",tags = "频道管理API接口")
@RestController
@RequestMapping("/api/v1/channel")
public class ChannelController {
    //注入service
    @Autowired
    private ChannelService adminService;

    /**
     * 分页查询频道信息
     * @return
     */
    @ApiOperation(value = "分页条件查询频道信息")
    @PostMapping("/list")
    public ResponseResult<AdChannel> findChannelByPage(@RequestBody ChannelDTO channelDTO){
        return adminService.findChannelByPage(channelDTO);
    }


    /**
     * 新增频道
     * @param channel 前端传输的频道数据
     * @return
     */
    @ApiOperation(value = "新增频道")
    @PostMapping("/save")
    public ResponseResult saveChannel(@RequestBody @Validated AdChannel channel ){
        return adminService.saveChannel(channel);
    }

    /**
     * 修改频道信息
     * @param channel 修改后的频道参数
     * @return
     */
    @ApiOperation(value = "修改频道")
    @PostMapping("/update")
    public ResponseResult updateChannel(@RequestBody AdChannel channel){
        return adminService.updateChannel(channel);
    }


    /**
     * 根据频道id删除频道
     * @param id 频道id
     * @return
     */
    @ApiOperation(value = "删除频道")
    @GetMapping("/del/{id}")
    public ResponseResult deleteChannel(@PathVariable Integer id){
        return adminService.deleteChannel(id);
    }

    @ApiOperation("查询全部频道")
    @GetMapping("/channels")
    public ResponseResult findAll() {
        List<AdChannel> list = adminService.list();
        return ResponseResult.okResult(list);
    }

    @ApiOperation("根据id查询频道")
    @GetMapping("/one/{id}")
    public ResponseResult findOne(@PathVariable Integer id) {
        return ResponseResult.okResult(adminService.getById(id));
    }
}
